package org.liveboardgames.agricola.control.delegates
{
    import com.adobe.cairngorm.business.ServiceLocator;
    
    import mx.collections.ListCollectionView;
    import mx.rpc.IResponder;
    
    import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
    import org.liveboardgames.agricola.model.AgricolaModelLocator;
    import org.liveboardgames.agricola.vo.Game;
    import org.liveboardgames.agricola.vo.action.Action;
    import org.liveboardgames.agricola.vo.card.ImprovementCard;
    import org.liveboardgames.agricola.vo.common.Target;
    import org.liveboardgames.agricola.vo.player.Player;
    import org.liveboardgames.common.dictionary.collections.IMap;
    
    public class BuildImprovementDelegate
    {
        private var _responder:IResponder;
        
        private var _service:Object;
        
        public function BuildImprovementDelegate(responder:IResponder)
        {
            this._service = ServiceLocator.getInstance().getRemoteObject("majorImprovementsService");
			this._responder = responder;
        }
        
        public function buildImprovement(player:Player, cost:IMap, improvement:ImprovementCard):void
        {
        	// TODO: at this point, the cost needs to be the one that will effectively be paid
        	var call:Object = _service.buildImprovement(AgricolaModelLocator.getInstance().game.id, player.id, cost.toObject(), improvement.cardId);
			call.resultHandler = _responder.result;
			call.faultHandler = _responder.fault;
//            setTimeout(buildImprovementService, 0, player, cost, improvement);
        }
        
        private function buildImprovementService(player:Player, cost:IMap, improvement:ImprovementCard):void
        {
            var game:Game = AgricolaModelLocator.getInstance().game;
            
            // Pay - by whatever means was chosen
            GameHelper.pay(player.farm, cost, game);
            
            // Add the improvement to the player's belongings
            player.farm.addPlayedCard(improvement);
            
            // And remove it from the game's improvements
            var index:int = game.majorImprovements.getItemIndex(improvement);
            game.majorImprovements.removeItemAt(index);
            
            // Get the possible immediate benefits of the card (i.e. the actions that 
            // it immediately grants)
            // TODO: likely an issue with this: we modify the model behind the scenes. 
            // This absolutely needs to be reflected in the DB
            for each (var cardAction:Action in improvement.actions)
            {
                // If the action does not have a target set up yet, 
                // add the player
                var target:Target = cardAction.target;
                if (target == null)
                {
                    cardAction.target = player;
                }
            }
            var additionalActions:ListCollectionView = GameHelper.getPlayTriggeredActions(improvement);
            AgricolaModelLocator.getInstance().actionController.addNewActionsInQueue(additionalActions, player);
        
        }
    }
}